#include "stdafx.h"
#include "ModelSearchBar.h"

ModelSearchBar::ModelSearchBar( QWidget * parent ) : QLineEdit( parent ), mList( NULL ), mResources( NULL )
{
	connect( (QLineEdit*)this, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)) );
}
ModelSearchBar::~ModelSearchBar( )
{}

void ModelSearchBar::setupBuddies( Lit::ResourcesList* list, QListWidget * widget )
{
	mList = widget;
	mResources = list;
}
void ModelSearchBar::textChanged( const QString &txt )
{
	if( !mList ) return;
	mList->clear();
	std::string str = txt.toAscii().data();
	if( str.empty() ) str = "*";

	if( !Ogre::StringUtil::startsWith(str, "*") ) str = "*" + str;
	if( !Ogre::StringUtil::endsWith(str, "*") ) str = str + "*";

	for( auto i=mResources->meshes().begin(); i!=mResources->meshes().end(); i++ )
	{
		if( Ogre::StringUtil::match( i->getName(), str, false ) )
			mList->addItem( i->getName().c_str() );
	}
}